package cj.web.admin.controllers;

import java.util.List;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.DateUtils;
import cj.web.admin.dao.FeedbackDAO;
import cj.web.admin.dao.FeedbackReplyDAO;
import cj.web.admin.domain.Feedback;
import cj.web.admin.domain.FeedbackReply;
import cj.web.constants.MemuEnum;

@Path("/feedbackReply")
public class FeedbackReplyController{

	private static Logger logger = Logger.getLogger(FeedbackReplyController.class.getName());
	
	@Autowired
	private FeedbackDAO feedbackDao;
	@Autowired
	private FeedbackReplyDAO feedbackReplyDao;
	
	@Get("show")
	public String show(Model model,@Param("feedbackId") int feedbackId) {
	
		Feedback obj = feedbackDao.queryById(feedbackId);
		
		if(obj!=null)
		{
			FeedbackReply feedbackReply = new FeedbackReply();
			feedbackReply.setFeedbackId(feedbackId);
			feedbackReplyDao.query(feedbackReply);
		}
		
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgwdyplgl);
        model.add("feedbackId",feedbackId);
		return "/views/news/feedbackReplyList.vm";
	}

	@Post("getList")
	public String queryList(FeedbackReply feedbackReply) {
		
		Feedback feedback = feedbackDao.queryById(feedbackReply.getFeedbackId());
	
		List<FeedbackReply> arr = feedbackReplyDao.query(feedbackReply);
		
		for(FeedbackReply obj:arr)
		{
			obj.setFeedbackUserName(feedback.getCreateUser());
			obj.setFeedbackDescs(feedback.getMsg());
			obj.setCreateAt(DateUtils.getMinusTime(obj.getCreateAt(), DateUtils.DATE_FULL_STR, 0));
			if(obj.getUpdateAt()!=null){
				obj.setUpdateAt(DateUtils.getMinusTime(obj.getUpdateAt(), DateUtils.DATE_FULL_STR, 0));
			}else{
				obj.setUpdateAt("");
			}
		}

		JSONObject jo = new JSONObject();

		int size = feedbackReplyDao.querySize(feedbackReply);

		jo.put("data", arr);
		jo.put("draw", feedbackReply.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@"+jo.toString();
	}
	
	//置顶
	@Get("stick")
    public String stick(@Param("id") int id) {
		
		FeedbackReply obj = feedbackReplyDao.queryById(id);
		
		if(obj!=null)
		{
			feedbackReplyDao.updateByStickTime(id);
		}
		
		return "r:show?feedbackId="+obj.getFeedbackId();
	}
	
	//取消置顶
	@Get("channelStick")
    public String channelStick(@Param("id") int id) {
		
		FeedbackReply obj = feedbackReplyDao.queryById(id);
		
		if(obj!=null)
		{
			feedbackReplyDao.updateByStickTimeIsNull(id);
		}
		
		return "r:show?feedbackId="+obj.getFeedbackId();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id)
	{
		FeedbackReply obj = feedbackReplyDao.queryById(id);
		
		if(obj!=null)
		{
			feedbackReplyDao.deleteById(id);
		}
		
		return "r:show?feedbackId="+obj.getFeedbackId();
	}
}
